home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / mint / utilit~1 / gdb35src.zoo / dist-gdb / atari-dep.c.old < prev    next >
Encoding:
Text File  |  1993-03-02  |  19.4 KB  |  727 lines

  1. /* Machine-dependent code which would otherwise be in inflow.c and core.c,
  2.    for GDB, the GNU debugger.
  3.    Copyright (C) 1986, 1987, 1989 Free Software Foundation, Inc.
  4.  
  5. This file is part of GDB.
  6.  
  7. GDB is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 1, or (at your option)
  10. any later version.
  11.  
  12. GDB is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with GDB; see the file COPYING.  If not, write to
  19. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  20.  
  21. /* This one is a try to use the MiNT PROCFS for debugging. I simply re-hacked
  22.     all calls to ptrace into calls to Fopen/Fclose/Fcntl. I really hope this
  23.     works, but it's a bit messy. At best, *VERY* little error checking is done.
  24.     I should really do this some time... all changes made via #ifdef atarist
  25.  
  26.     Questions and bugfixes welcome. Send mail to
  27.  
  28.     Joerg.Hessdoerfer@EUROPA.rs.kp.dlr.de (preferred 'till May '93)
  29.     Hessdorf@sun.ph-cip.uni-koeln.de
  30.  
  31. */
  32.     
  33. #include <stdio.h>
  34. #include "defs.h"
  35. #include "param.h"
  36. #include "frame.h"
  37. #include "inferior.h"
  38.  
  39. #include <sys/param.h>
  40. #include <sys/dir.h>
  41. #include <signal.h>
  42. #include <sys/ioctl.h>
  43. #include <fcntl.h>
  44.  
  45. #ifndef atarist
  46. #include <sys/ptrace.h>
  47. #include <machine/reg.h>
  48. #include <sys/user.h>
  49. #include <sys/core.h>
  50. #else
  51. #define PCTXTSIZE    (('P'<< 8) | 3)
  52. #define PSETFLAGS    (('P'<< 8) | 4)
  53. #define PGETFLAGS    (('P'<< 8) | 5)
  54. #define PTRACESFLAGS    (('P'<< 8) | 6)
  55. #define PTRACEGFLAGS    (('P'<< 8) | 7)
  56. #    define    P_ENABLE    (1 << 0)    /* enable tracing */
  57.  
  58. #define PTRACEGO    (('P'<< 8) | 8)
  59. #define PTRACEFLOW    (('P'<< 8) | 9)
  60. #define PTRACESTEP    (('P'<< 8) | 10)
  61. #define PTRACE11    (('P'<< 8) | 11)    /* unused, reserved */
  62.  
  63. #define SHMGETBLK    (('M'<< 8) | 0)
  64. #define SHMSETBLK    (('M'<< 8) | 1)
  65. #endif
  66.  
  67. #include <a.out.h>
  68. #include <sys/file.h>
  69. #include <sys/stat.h>
  70.  
  71. extern int errno;
  72. extern int attach_flag;
  73.  
  74.  
  75. #ifdef atarist
  76. static struct
  77. {
  78.     int ProcPid;
  79.     int ProcHandle;
  80.     char ProcName[64];
  81. }ProcList[4]={ {0,0,""}, {0,0,""}, {0,0,""}, {0,0,""} };
  82.  
  83. static int ProcIndex; /* used for current index hold */
  84.  
  85. static int LookUpHandle(pid)
  86. int pid;
  87. {
  88.     int i;
  89.     for(i=0;i<4 && ProcList[i].ProcPid!=pid;i++);
  90.     return i;
  91. }
  92.  
  93. static int StuffInHandle(pid,fh,name)
  94. int pid,fh;
  95. char *name;
  96. {
  97.     int i;
  98.  
  99.     for(i=0;i<4 && ProcPid!=0;i++);
  100.     if(i==4)return -1; /* no more space left! */
  101.     ProcList[i].ProcPid=pid;
  102.     ProcList[i].ProcHandle=fh;
  103.     strcpy(ProcList[i].ProcName,name);
  104.     return 0;
  105. }
  106.     
  107. #endif
  108.  
  109. #ifndef atarist
  110. /* This function simply calls ptrace with the given arguments.  
  111.    It exists so that all calls to ptrace are isolated in this 
  112.    machine-dependent file. */
  113. int
  114. call_ptrace (request, pid, arg3, arg4)
  115.      int request, pid, arg3, arg4;
  116. {
  117.   return ptrace (request, pid, arg3, arg4);
  118. }
  119. #endif
  120.  
  121. kill_inferior ()
  122. {
  123.   if (remote_debugging)
  124.     return;
  125.   if (inferior_pid == 0)
  126.     return;
  127.  
  128. #ifndef atarist
  129.   ptrace (8, inferior_pid, 0, 0);
  130. #else
  131.     ProcIndex=LookUpHandle(inferior_pid);
  132.     Fclose(ProcList[ProcIndex].ProcHandle);
  133.     Fdelete(ProcList[ProcIndex].ProcName);
  134.     ProcList[ProcIndex].ProcPid=0; /* mark entry as free again */
  135. #endif
  136.     
  137.   wait (0);
  138.   inferior_died ();
  139. }
  140.  
  141. /* This is used when GDB is exiting.  It gives less chance of error.*/
  142.  
  143. kill_inferior_fast ()
  144. {
  145.   if (remote_debugging)
  146.     return;
  147.   if (inferior_pid == 0)
  148.     return;
  149. #ifndef atarist
  150.   ptrace (8, inferior_pid, 0, 0);
  151. #else
  152.     ProcIndex=LookUpHandle(inferior_pid);
  153.     Fclose(ProcList[ProcIndex].ProcHandle);
  154.     Fdelete(ProcList[ProcIndex].ProcName);
  155.     ProcList[ProcIndex].ProcPid=0; /* mark entry as free again */
  156. #endif
  157.     
  158.   wait (0);
  159. }
  160.  
  161. /* Resume execution of the inferior process.
  162.    If STEP is nonzero, single-step it.
  163.    If SIGNAL is nonzero, give it that signal.  */
  164.  
  165. void
  166. resume (step, signal)
  167.      int step;
  168.      int signal;
  169. {
  170.   errno = 0;
  171.   if (remote_debugging)
  172.     remote_resume (step, signal);
  173.   else
  174.     {
  175. #ifndef atarist
  176.       ptrace (step ? 9 : 7, inferior_pid, 1, signal);
  177. #else
  178.         ProcIndex=LookUpHandle(inferior_pid);
  179.         errno=Fcntl(ProcList[ProcIndex].ProcHandle,&((short)signal),step ? PTRACESTEP : PTRACEGO);
  180. #endif
  181.       if (errno)
  182.     perror_with_name ("ptrace");
  183.     }
  184. }
  185.  
  186. #ifdef ATTACH_DETACH
  187.  
  188. /* Start debugging the process whose number is PID.  */
  189.  
  190. attach (pid)
  191.      int pid;
  192. {
  193.   errno = 0;
  194. #ifndef atarist
  195.   ptrace (PTRACE_ATTACH, pid, 0, 0);
  196. #else
  197. {
  198.     char pname[64];
  199.     int fh;
  200.     
  201.     sprintf(pname,"U:\\PROC\\.%03d",pid);
  202.     fh=Fopen(pname,2);
  203.     if(fh>0)
  204.     {
  205.         errno=Fcntl(fh,P_ENABLE,PTRACESFLAGS);
  206.         if(!errno)StuffInHandle(pid,fh,pname);
  207.     }
  208. }
  209. #endif
  210.  
  211.   if (errno)
  212.     perror_with_name ("ptrace");
  213.   attach_flag = 1;
  214.   return pid;
  215. }
  216.  
  217. /* Stop debugging the process whose number is PID
  218.    and continue it with signal number SIGNAL.
  219.    SIGNAL = 0 means just continue it.  */
  220.  
  221. void
  222. detach (signal)
  223.      int signal;
  224. {
  225.   errno = 0;
  226.  
  227. #ifndef atarist
  228.   ptrace (PTRACE_DETACH, inferior_pid, 1, signal);
  229. #else
  230.     ProcIndex=LookUpHandle(inferior_pid);
  231.     errno=Fcntl(ProcList[ProcIndex].ProcHandle,0,PTRACESFLAGS);
  232.     Fclose(ProcList[ProcIndex].ProcHandle);
  233.     ProcList[ProcIndex].ProcPid=0;
  234. #endif
  235.  
  236.   if (errno)
  237.     perror_with_name ("ptrace");
  238.   attach_flag = 0;
  239. }
  240. #endif /* ATTACH_DETACH */
  241.  
  242. void
  243. fetch_inferior_registers ()
  244. {
  245.   struct regs inferior_registers;
  246. #ifdef FP0_REGNUM
  247.   struct fp_status inferior_fp_registers;
  248. #endif
  249.   extern char registers[];
  250.  
  251.   if (remote_debugging)
  252.     remote_fetch_registers (registers);
  253.   else
  254.     {
  255. #ifndef atarist
  256.       ptrace (PTRACE_GETREGS, inferior_pid, &inferior_registers);
  257. #ifdef FP0_REGNUM
  258.       ptrace (PTRACE_GETFPREGS, inferior_pid, &inferior_fp_registers);
  259. #endif
  260. #else
  261.         ProcIndex=LookUpHandle(inferior_pid);
  262.  
  263.       bcopy (&inferior_registers, registers, 16 * 4);
  264. #ifdef FP0_REGNUM
  265.       bcopy (&inferior_fp_registers, ®isters[REGISTER_BYTE (FP0_REGNUM)],
  266.          sizeof inferior_fp_registers.fps_regs);
  267. #endif 
  268.       *(int *)®isters[REGISTER_BYTE (PS_REGNUM)] = inferior_registers.r_ps;
  269.       *(int *)®isters[REGISTER_BYTE (PC_REGNUM)] = inferior_registers.r_pc;
  270. #ifdef FP0_REGNUM
  271.       bcopy (&inferior_fp_registers.fps_control,
  272.          ®isters[REGISTER_BYTE (FPC_REGNUM)],
  273.          sizeof inferior_fp_registers - sizeof inferior_fp_registers.fps_regs);
  274. #endif 
  275.     }
  276. }
  277.  
  278. /* Store our register values back into the inferior.
  279.    If REGNO is -1, do this for all registers.
  280.    Otherwise, REGNO specifies which register (so we can save time).  */
  281.  
  282. store_inferior_registers (regno)
  283.      int regno;
  284. {
  285.   struct regs inferior_registers;
  286.   struct fp_status inferior_fp_registers;
  287.   extern char registers[];
  288.  
  289.   if (remote_debugging)
  290.     remote_store_registers (registers);
  291.   else
  292.     {
  293.       bcopy (registers, &inferior_registers, 16 * 4);
  294. #ifdef FP0_REGNUM
  295.       bcopy (®isters[REGISTER_BYTE (FP0_REGNUM)], &inferior_fp_registers,
  296.          sizeof inferior_fp_registers.fps_regs);
  297. #endif
  298.       inferior_registers.r_ps = *(int *)®isters[REGISTER_BYTE (PS_REGNUM)];
  299.       inferior_registers.r_pc = *(int *)®isters[REGISTER_BYTE (PC_REGNUM)];
  300.  
  301. #ifdef FP0_REGNUM
  302.       bcopy (®isters[REGISTER_BYTE (FPC_REGNUM)],
  303.          &inferior_fp_registers.fps_control,
  304.          sizeof inferior_fp_registers - sizeof inferior_fp_registers.fps_regs);
  305. #endif
  306.  
  307.       ptrace (PTRACE_SETREGS, inferior_pid, &inferior_registers);
  308. #if FP0_REGNUM
  309.       ptrace (PTRACE_SETFPREGS, inferior_pid, &inferior_fp_registers);
  310. #endif
  311.     }
  312. }
  313.  
  314. /* NOTE! I tried using PTRACE_READDATA, etc., to read and write memory
  315.    in the NEW_SUN_PTRACE case.
  316.    It ought to be straightforward.  But it appears that writing did
  317.    not write the data that I specified.  I cannot understand where
  318.    it got the data that it actually did write.  */
  319.  
  320. /* Copy LEN bytes from inferior's memory starting at MEMADDR
  321.    to debugger memory starting at MYADDR. 
  322.    On failure (cannot read from inferior, usually because address is out
  323.    of bounds) returns the value of errno. */
  324.  
  325. int
  326. read_inferior_memory (memaddr, myaddr, len)
  327.      CORE_ADDR memaddr;
  328.      char *myaddr;
  329.      int len;
  330. {
  331.   register int i;
  332.   /* Round starting address down to longword boundary.  */
  333.   register CORE_ADDR addr = memaddr & - sizeof (int);
  334.   /* Round ending address up; get number of longwords that makes.  */
  335.   register int count
  336.     = (((memaddr + len) - addr) + sizeof (int) - 1) / sizeof (int);
  337.   /* Allocate buffer of that many longwords.  */
  338.   register int *buffer = (int *) alloca (count * sizeof (int));
  339.   extern int errno;
  340.  
  341.   /* Read all the longwords */
  342.   for (i = 0; i < count; i++, addr += sizeof (int))
  343.     {
  344.       errno = 0;
  345.       if (remote_debugging)
  346.     buffer[i] = remote_fetch_word (addr);
  347.       else
  348.     buffer[i] = ptrace (1, inferior_pid, addr, 0);
  349.       if (errno)
  350.     return errno;
  351.     }
  352.  
  353.   /* Copy appropriate bytes out of the buffer.  */
  354.   bcopy ((char *) buffer + (memaddr & (sizeof (int) - 1)), myaddr, len);
  355.   return 0;
  356. }
  357.  
  358. /* Copy LEN bytes of data from debugger memory at MYADDR
  359.    to inferior's memory at MEMADDR.
  360.    On failure (cannot write the inferior)
  361.    returns the value of errno.  */
  362.  
  363. int
  364. write_inferior_memory (memaddr, myaddr, len)
  365.      CORE_ADDR memaddr;
  366.      char *myaddr;
  367.      int len;
  368. {
  369.   register int i;
  370.   /* Round starting address down to longword boundary.  */
  371.   register CORE_ADDR addr = memaddr & - sizeof (int);
  372.   /* Round ending address up; get number of longwords that makes.  */
  373.   register int count
  374.     = (((memaddr + len) - addr) + sizeof (int) - 1) / sizeof (int);
  375.   /* Allocate buffer of that many longwords.  */
  376.   register int *buffer = (int *) alloca (count * sizeof (int));
  377.   extern int errno;
  378.  
  379.   /* Fill start and end extra bytes of buffer with existing memory data.  */
  380.  
  381.   if (remote_debugging)
  382.     buffer[0] = remote_fetch_word (addr);
  383.   else
  384.     buffer[0] = ptrace (1, inferior_pid, addr, 0);
  385.  
  386.   if (count > 1)
  387.     {
  388.       if (remote_debugging)
  389.     buffer[count - 1]
  390.       = remote_fetch_word (addr + (count - 1) * sizeof (int));
  391.       else
  392.     buffer[count - 1]
  393.       = ptrace (1, inferior_pid,
  394.             addr + (count - 1) * sizeof (int), 0);
  395.     }
  396.  
  397.   /* Copy data to be written over corresponding part of buffer */
  398.  
  399.   bcopy (myaddr, (char *) buffer + (memaddr & (sizeof (int) - 1)), len);
  400.  
  401.   /* Write the entire buffer.  */
  402.  
  403.   for (i = 0; i < count; i++, addr += sizeof (int))
  404.     {
  405.       errno = 0;
  406.       if (remote_debugging)
  407.     remote_store_word (addr, buffer[i]);
  408.       else
  409.     ptrace (4, inferior_pid, addr, buffer[i]);
  410.       if (errno)
  411.     return errno;
  412.     }
  413.  
  414.   return 0;
  415. }
  416.  
  417.  
  418. /* Machine-dependent code which would otherwise be in core.c */
  419. /* Work with core dump and executable files, for GDB. */
  420.  
  421. #ifndef N_TXTADDR
  422. #define N_TXTADDR(hdr) 0
  423. #endif /* no N_TXTADDR */
  424.  
  425. #ifndef N_DATADDR
  426. #define N_DATADDR(hdr) hdr.a_text
  427. #endif /* no N_DATADDR */
  428.  
  429. /* Non-zero if this is an object (.o) file, rather than an executable.
  430.    Distinguishing between the two is rarely necessary (and seems like
  431.    a hack, but there is no other way to get the text and data
  432.    addresses--N_TXTADDR should probably take care of
  433.    this, but it doesn't).  */
  434. /* This definition will not work
  435.    if someone decides to make ld preserve relocation info.  */
  436. #define IS_OBJECT_FILE(hdr) (hdr.a_trsize != 0)
  437.   
  438. /* Make COFF and non-COFF names for things a little more compatible
  439.    to reduce conditionals later.  */
  440.  
  441. #ifdef COFF_FORMAT
  442. #define a_magic magic
  443. #endif
  444.  
  445. #ifndef COFF_FORMAT
  446. #ifndef AOUTHDR
  447. #define AOUTHDR struct exec
  448. #endif
  449. #endif
  450.  
  451. extern char *sys_siglist[];
  452.  
  453. /* Hook for `exec_file_command' command to call.  */
  454.  
  455. extern void (*exec_file_display_hook) ();
  456.    
  457. /* File names of core file and executable file.  */
  458.  
  459. extern char *corefile;
  460. extern char *execfile;
  461.  
  462. /* Descriptors on which core file and executable file are open.
  463.    Note that the execchan is closed when an inferior is created
  464.    and reopened if the inferior dies or is killed.  */
  465.  
  466. extern int corechan;
  467. extern int execchan;
  468.  
  469. /* Last modification time of executable file.
  470.    Also used in source.c to compare against mtime of a source file.  */
  471.  
  472. extern int exec_mtime;
  473.  
  474. /* Virtual addresses of bounds of the two areas of memory in the core file.  */
  475.  
  476. extern CORE_ADDR data_start;
  477. extern CORE_ADDR data_end;
  478. extern CORE_ADDR stack_start;
  479. extern CORE_ADDR stack_end;
  480.  
  481. /* Virtual addresses of bounds of two areas of memory in the exec file.
  482.    Note that the data area in the exec file is used only when there is no core file.  */
  483.  
  484. extern CORE_ADDR text_start;
  485. extern CORE_ADDR text_end;
  486.  
  487. extern CORE_ADDR exec_data_start;
  488. extern CORE_ADDR exec_data_end;
  489.  
  490. /* Address in executable file of start of text area data.  */
  491.  
  492. extern int text_offset;
  493.  
  494. /* Address in executable file of start of data area data.  */
  495.  
  496. extern int exec_data_offset;
  497.  
  498. /* Address in core file of start of data area data.  */
  499.  
  500. extern int data_offset;
  501.  
  502. /* Address in core file of start of stack area data.  */
  503.  
  504. extern int stack_offset;
  505.   
  506. #ifdef COFF_FORMAT
  507. /* various coff data structures */
  508.  
  509. extern FILHDR file_hdr;
  510. extern SCNHDR text_hdr;
  511. extern SCNHDR data_hdr;
  512.  
  513. #endif /* not COFF_FORMAT */
  514.  
  515. /* a.out header saved in core file.  */
  516.   
  517. extern AOUTHDR core_aouthdr;
  518.  
  519. /* a.out header of exec file.  */
  520.  
  521. extern AOUTHDR exec_aouthdr;
  522.  
  523. extern void validate_files ();
  524.  
  525. core_file_command (filename, from_tty)
  526.      char *filename;
  527.      int from_tty;
  528. {
  529.   int val;
  530.   extern char registers[];
  531.  
  532.   /* Discard all vestiges of any previous core file
  533.      and mark data and stack spaces as empty.  */
  534.  
  535.   if (corefile)
  536.     free (corefile);
  537.   corefile = 0;
  538.  
  539.   if (corechan >= 0)
  540.     close (corechan);
  541.   corechan = -1;
  542.  
  543.   data_start = 0;
  544.   data_end = 0;
  545.   stack_start = STACK_END_ADDR;
  546.   stack_end = STACK_END_ADDR;
  547.  
  548.   /* Now, if a new core file was specified, open it and digest it.  */
  549.  
  550.   if (filename)
  551.     {
  552.       filename = tilde_expand (filename);
  553.       make_cleanup (free, filename);
  554.       
  555.       if (have_inferior_p ())
  556.     error ("To look at a core file, you must kill the inferior with \"kill\".");
  557.       corechan = open (filename, O_RDONLY, 0);
  558.       if (corechan < 0)
  559.     perror_with_name (filename);
  560.  
  561.       {
  562.     struct core corestr;
  563.  
  564.     val = myread (corechan, &corestr, sizeof corestr);
  565.     if (val < 0)
  566.       perror_with_name (filename);
  567.     if (corestr.c_magic != CORE_MAGIC)
  568.       error ("\"%s\" does not appear to be a core dump file (magic 0x%x, expected 0x%x)",
  569.          filename, corestr.c_magic, (int) CORE_MAGIC);
  570.     else if (sizeof (struct core) != corestr.c_len)
  571.       error ("\"%s\" has an invalid struct core length (%d, expected %d)",
  572.          filename, corestr.c_len, (int) sizeof (struct core));
  573.  
  574.     data_start = exec_data_start;
  575.     data_end = data_start + corestr.c_dsize;
  576.     stack_start = stack_end - corestr.c_ssize;
  577.     data_offset = sizeof corestr;
  578.     stack_offset = sizeof corestr + corestr.c_dsize;
  579.  
  580.     bcopy (&corestr.c_regs, registers, 16 * 4);
  581.     *(int *)®isters[REGISTER_BYTE (PS_REGNUM)] = corestr.c_regs.r_ps;
  582.     *(int *)®isters[REGISTER_BYTE (PC_REGNUM)] = corestr.c_regs.r_pc;
  583. #ifdef FP0_REGNUM
  584. #ifdef FPU
  585.      bcopy (corestr.c_fpu.f_fpstatus.fps_regs,
  586.             ®isters[REGISTER_BYTE (FP0_REGNUM)],
  587.             sizeof corestr.c_fpu.f_fpstatus.fps_regs);
  588.      bcopy (&corestr.c_fpu.f_fpstatus.fps_control,
  589.             ®isters[REGISTER_BYTE (FPC_REGNUM)],
  590.             sizeof corestr.c_fpu.f_fpstatus - sizeof corestr.c_fpu.f_fpstatus.fps_regs);
  591. #else
  592.     bcopy (corestr.c_fpstatus.fps_regs,
  593.            ®isters[REGISTER_BYTE (FP0_REGNUM)],
  594.            sizeof corestr.c_fpstatus.fps_regs);
  595.     bcopy (&corestr.c_fpstatus.fps_control,
  596.            ®isters[REGISTER_BYTE (FPC_REGNUM)],
  597.            sizeof corestr.c_fpstatus - sizeof corestr.c_fpstatus.fps_regs);
  598. #endif
  599. #endif
  600.     bcopy (&corestr.c_aouthdr, &core_aouthdr, sizeof (struct exec));
  601.  
  602.     printf ("Core file is from \"%s\".\n", corestr.c_cmdname);
  603.     if (corestr.c_signo > 0)
  604.       printf ("Program terminated with signal %d, %s.\n",
  605.             corestr.c_signo,
  606.             corestr.c_signo < NSIG
  607.             ? sys_siglist[corestr.c_signo]
  608.             : "(undocumented)");
  609.       }
  610.       if (filename[0] == '/')
  611.     corefile = savestring (filename, strlen (filename));
  612.       else
  613.     {
  614.       corefile = concat (current_directory, "/", filename);
  615.     }
  616.  
  617.       set_current_frame ( create_new_frame (read_register (FP_REGNUM),
  618.                         read_pc ()));
  619.       select_frame (get_current_frame (), 0);
  620.       validate_files ();
  621.     }
  622.   else if (from_tty)
  623.     printf ("No core file now.\n");
  624. }
  625.  
  626. exec_file_command (filename, from_tty)
  627.      char *filename;
  628.      int from_tty;
  629. {
  630.   int val;
  631.  
  632.   /* Eliminate all traces of old exec file.
  633.      Mark text segment as empty.  */
  634.  
  635.   if (execfile)
  636.     free (execfile);
  637.   execfile = 0;
  638.   data_start = 0;
  639.   data_end -= exec_data_start;
  640.   text_start = 0;
  641.   text_end = 0;
  642.   exec_data_start = 0;
  643.   exec_data_end = 0;
  644.   if (execchan >= 0)
  645.     close (execchan);
  646.   execchan = -1;
  647.  
  648.   /* Now open and digest the file the user requested, if any.  */
  649.  
  650.   if (filename)
  651.     {
  652.       filename = tilde_expand (filename);
  653.       make_cleanup (free, filename);
  654.       
  655.       execchan = openp (getenv ("PATH"), 1, filename, O_RDONLY, 0,
  656.             &execfile);
  657.       if (execchan < 0)
  658.     perror_with_name (filename);
  659.  
  660. #ifdef COFF_FORMAT
  661.       {
  662.     int aout_hdrsize;
  663.     int num_sections;
  664.  
  665.     if (read_file_hdr (execchan, &file_hdr) < 0)
  666.       error ("\"%s\": not in executable format.", execfile);
  667.  
  668.     aout_hdrsize = file_hdr.f_opthdr;
  669.     num_sections = file_hdr.f_nscns;
  670.  
  671.     if (read_aout_hdr (execchan, &exec_aouthdr, aout_hdrsize) < 0)
  672.       error ("\"%s\": can't read optional aouthdr", execfile);
  673.  
  674.     if (read_section_hdr (execchan, _TEXT, &text_hdr, num_sections,
  675.                   aout_hdrsize) < 0)
  676.       error ("\"%s\": can't read text section header", execfile);
  677.  
  678.     if (read_section_hdr (execchan, _DATA, &data_hdr, num_sections,
  679.                   aout_hdrsize) < 0)
  680.       error ("\"%s\": can't read data section header", execfile);
  681.  
  682.     text_start = exec_aouthdr.text_start;
  683.     text_end = text_start + exec_aouthdr.tsize;
  684.     text_offset = text_hdr.s_scnptr;
  685.     exec_data_start = exec_aouthdr.data_start;
  686.     exec_data_end = exec_data_start + exec_aouthdr.dsize;
  687.     exec_data_offset = data_hdr.s_scnptr;
  688.     data_start = exec_data_start;
  689.     data_end += exec_data_start;
  690.     exec_mtime = file_hdr.f_timdat;
  691.       }
  692. #else /* not COFF_FORMAT */
  693.       {
  694.     struct stat st_exec;
  695.     val = myread (execchan, &exec_aouthdr, sizeof (AOUTHDR));
  696.  
  697.     if (val < 0)
  698.       perror_with_name (filename);
  699.  
  700.     text_start =
  701.       IS_OBJECT_FILE (exec_aouthdr) ? 0 : N_TXTADDR (exec_aouthdr);
  702.         exec_data_start = IS_OBJECT_FILE (exec_aouthdr)
  703.       ? exec_aouthdr.a_text : N_DATADDR (exec_aouthdr);
  704.     text_offset = N_TXTOFF (exec_aouthdr);
  705.     exec_data_offset = N_TXTOFF (exec_aouthdr) + exec_aouthdr.a_text;
  706.  
  707.     text_end = text_start + exec_aouthdr.a_text;
  708.         exec_data_end = exec_data_start + exec_aouthdr.a_data;
  709.     data_start = exec_data_start;
  710.     data_end += exec_data_start;
  711.  
  712.     fstat (execchan, &st_exec);
  713.     exec_mtime = st_exec.st_mtime;
  714.       }
  715. #endif /* not COFF_FORMAT */
  716.  
  717.       validate_files ();
  718.     }
  719.   else if (from_tty)
  720.     printf ("No exec file now.\n");
  721.  
  722.   /* Tell display code (if any) about the changed file name.  */
  723.   if (exec_file_display_hook)
  724.     (*exec_file_display_hook) (filename);
  725. }
  726.  
  727.